local sk__xiangsheng = fk.CreateSkill {

  name = "sk__xiangsheng",

  tags = { Skill.Compulsory, },

}

sk__xiangsheng:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  if not player.dead then
    room:addPlayerMark(player, "@sk__xiangsheng", 6)
  end
end)
sk__xiangsheng:addLoseEffect(function(self, player, is_death)
  local room = player.room
  if not player.dead then
    local n = player:getMark("@sk__xiangsheng")
    room:setPlayerMark(player, "@sk__xiangsheng", 0)
    if n > 0 then
      player:drawCards(n, sk__xiangsheng.name)
    end
  end
end)

sk__xiangsheng:addEffect(fk.AfterCardsMove, {
  name = "sk__xiangsheng",
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__xiangsheng.name) and player:getMark("@sk__xiangsheng") > 0 then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
            if player.dead or player:getMark("@sk__xiangsheng") == 0 then return end
            local prompt = ""
            if Fk:getCardById(info.cardId).color == Card.Black then
              prompt = "#sk__xiangsheng1-invoke"
            elseif Fk:getCardById(info.cardId).color == Card.Red then
              prompt = "#sk__xiangsheng2-invoke"
            end
            if prompt ~= "" and room:askForSkillInvoke(player, sk__xiangsheng.name, nil, prompt) then
              room:removePlayerMark(player, "@sk__xiangsheng", 1)
              local pattern = ".|.|spade,club"
              if prompt == "#sk__xiangsheng1-invoke" then
                pattern = ".|.|heart,diamond"
              end
              local cards = room:getCardsFromPileByRule(pattern, 1, "drawPile")
              if #cards > 0 then
                room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonDraw, sk__xiangsheng.name, nil, false,
                  player)
              end
            end
          end
        end
      end
    end
  end,
})

return sk__xiangsheng
